/**
 * Copyright (C) 2008 GeoScheduler Team, as stated on <http://www.geoscheduler.org/authors>.
 * 
 * This file is part of GeoScheduler.
 * 
 * GeoScheduler is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * GeoScheduler is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with GeoScheduler. If not, see <http://www.gnu.org/licenses/>.
 */

package org.geoscheduler.commons.msgs;

import java.util.Arrays;
import java.util.List;
import java.util.Locale;

import org.geoscheduler.commons.i18n.Localizable;
import org.geoscheduler.commons.i18n.LocalizationSource;
import org.geoscheduler.commons.marshaller.JSONField;

/**
 * 
 * <h2>ParametrizedMessage</h2>
 * <p>
 * Implementation of {@link Message} interface.
 * </p>
 * 
 * @author lko
 */
public class ParametrizedMessage implements Message {

	private final String[] parameters;
	private final Enum<?> message;
	private final LocalizationSource locSrc;
	private final MessageSeverity severity;

	public ParametrizedMessage(Enum<?> message, String... parameters) {
		this(message, (message instanceof Localizable) ? ((Localizable) message).getLocalizationSource()
				: LocalizationSource.VOID_LOC_SOURCE, parameters);
	}

	public ParametrizedMessage(Enum<?> message, MessageSeverity severity, String... params) {
		this(message, (message instanceof Localizable) ? ((Localizable) message).getLocalizationSource()
				: LocalizationSource.VOID_LOC_SOURCE, severity, params);
	}

	public ParametrizedMessage(Enum<?> message, LocalizationSource locSrc, String... parameters) {
		this(message, locSrc, BasicMessageSeverities.ERROR, parameters);
	}

	public ParametrizedMessage(Enum<?> message, LocalizationSource locSrc, MessageSeverity severity,
			String... parameters) {
		super();
		this.parameters = parameters;
		this.message = message;
		this.severity = severity;
		this.locSrc = locSrc;
	}

	/**
	 * @see org.geoscheduler.commons.msgs.Message#getMessage()
	 */
	@JSONField
	public String getMessage() {
		return getMessage(Locale.getDefault());
	}

	/**
	 * @see org.geoscheduler.commons.msgs.Message#getMessage(java.util.Locale)
	 */
	public String getMessage(Locale locale) {
		return locSrc.translate(message.name(), locale);
	}

	/**
	 * Mock method. This is needed fot marshaller to work.
	 * 
	 * FIXME are we unmarshalling ParametrizedMessage? Than we may need this.
	 * 
	 * @param str
	 *            String that will be throwed out ..
	 */
	public void setMessage(String str) {
		throw new UnsupportedOperationException();
	}

	/**
	 * @see org.geoscheduler.commons.msgs.Message#getSeverity()
	 * 
	 * FIXME temporarily removed JSONField
	 */
	// @JSONField
	public MessageSeverity getSeverity() {
		return severity;
	}

	/**
	 * Mock method. This is needed fot marshaller to work.
	 * 
	 * FIXME are we unmarshalling ParametrizedMessage? Than we may need this.
	 * 
	 * @param sev
	 *            MessageSeverity that will not be used ..
	 */
	public void setSeverity(MessageSeverity sev) {
		throw new UnsupportedOperationException();
	}

	/**
	 * @see org.geoscheduler.commons.msgs.Message#getParameters()
	 */
	public List<String> getParameters() {
		return Arrays.asList(parameters);
	}

	/**
	 * @see org.geoscheduler.commons.msgs.Message#getType()
	 */
	@JSONField
	public Enum<?> getType() {
		return message;
	}

	/**
	 * Mock method. This is needed fot marshaller to work.
	 * 
	 * FIXME are we unmarshalling ParametrizedMessage? Than we may need this.
	 * 
	 * @param type
	 *            Type that will not be used ..
	 */
	public void setType(Enum<?> type) {
		throw new UnsupportedOperationException();
	}

	public LocalizationSource getLocalizationSource() {
		return this.locSrc;
	}
}